package com.ms.game.gmtool;

import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.service.common.EncryptHandler;
import com.xpec.c4.service.module.PropertyKey;
import com.xpec.c4.service.module.ServerProperties;

public class AppBillingVerify {
	private static String GOOGLE_PUBLIC_KEY = null;
	
	public static boolean Verify(String pid, Object... objects) {
		PurchasePlatformType platformType = PurchasePlatformType.valueOf(pid);
		boolean result = false;

		switch (platformType) {
		case GOOGLEPLAY:
			String purchase_data = String.valueOf(objects[0]);
			String signature = String.valueOf(objects[1]);
			result = GoogleIABCheck(purchase_data, signature);
			break;
		default:
			result = false;
			break;
		}
		
		return result;
	}

	/**
	 * 根據app的公鑰驗證訂單合法性
	 * 
	 * @param purchase_data
	 * @param signature
	 * @return
	 */
	private static boolean GoogleIABCheck(String purchase_data, String signature) {
		if(!GameUtility.isHaveString(GOOGLE_PUBLIC_KEY)){
			GOOGLE_PUBLIC_KEY = ServerProperties.getString(PropertyKey.google_purchase_public_key, "");
		}

		return RSASignature.doCheck(purchase_data, signature, GOOGLE_PUBLIC_KEY);
	}
	
	public static void main(String[] args) {
//		String info =  "{\"packageName\":\"com.Tonwa.Cash7\",\"productId\":\"psl14661456920398\",\"purchaseTime\":1466737635263,\"purchaseState\":0,\"purchaseToken\":\"dkmndhehcepfnkakohngbklj.AO-J1Oyu6XHUfg99mlCGj_VhMdE32RssWlzMdB8wMW4cESUdilgLD53s_7OvTQgRHxy-xabRDmSxj-WaHR_6sVe_rjbjCJW38KpNaj5ellx_4IGJRtPBSdCy_L2Sa7LmdcR9qK83KHDQ\"}";
//		String signString = "kK0mOOzpW8t5BRPpfcvnN+7o56oY0SphPT8tuHUmKVPkhwO/ZU2laU39w8Z/Dvq0jj5ep+V9vtbkB8WRhIKBJLwsRtdioMTztTGsEKI02GGMPb/cg6qFnOy2kWfVdGNu2p0NQrfZ1b+wBxGu7ccMKtWTjQshbpGW2q656YtAKOnOSe/F02OR8c60V8YaM2Bl3mvC3+XqcNZgS62jU+GJB1As+3bD/z7BgZWy6lr8xncva0U5S66dSbCfseAXt78o/jPxkSBWNF/2xMV/MiBRsvQFjRGSLOeDD/FvNTwtshgKZTOs0B5sNqGSrQ0QDc2miXd/gAOfhwPJ+9F6n5ZDCQ==";
//		System.out.println(Verify(PlatformType.GOOGLEPLAY.toString(), info, signString));
		
		System.out.println( EncryptHandler.md5("837cc999b909bee7ae0fc517f30a1af9" + "dgt29075654" + "1466737635263"));
	}
}
